package com.fourGirlsAndADude.mobileDRT;

import java.util.ArrayList;

import com.att.intern.webservice.Incident;
import com.att.intern.webservice.Webservice.TokenInvalidException;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class CreClosureOrDelayedOpenActivity extends Activity {
	// UI elements
	private TextView header;
	private TextView subHeader;
	private ListView listView;

	// Used to hold extras retrieved from intent
	private String token;
	private String eventName;
	private int year;

	// Used to display List
	private ArrayAdapter<String> displayList;
	private ArrayList<Incident> fromWebService = new ArrayList<Incident>();
	private ArrayList<String> buildingNamesToBeDisplayed = new ArrayList<String>();
	private String[] buildingInfoToBeDisplayed = new String[10];

	private String building;
	private String event = "Event: ";
	private String intialRequestDate = "Initial Request Date: ";
	private String buildingstatus = "Building Status: ";
	private String glc = "GLC: ";
	private String address = "Address: ";
	private String state = "State: ";
	private String type = "Type: ";
	private String pm = "PM: ";
	private String assessment = "Assessment: ";
	private String status = "Status: ";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cre_closure_or_delayed_open);
		token = getIntent().getStringExtra("SESSION_TOKEN");
		Log.v("TOKEN CHECK 3", "Token: " + token);
		eventName = getIntent().getStringExtra("EVENT_NAME");
		year = getIntent().getIntExtra("EVENT_YEAR", 0);
		Log.v("CCODOA", "values in");
		intializeArrayList(token, eventName, year);

		Log.v("CCODOA", "About to intialize text and list views");
		// Display TextView
		header = (TextView) findViewById(R.id.header);
		subHeader = (TextView) findViewById(R.id.damagerptresults);
		listView = (ListView) findViewById(R.id.closureOrDelayedListView);
		Log.v("CCODOA", "Text and list views created");
		// Create ArrayAdapter
		Log.v("CCODOA", "Grabbing building names");
		getBuildingNamesForDisplay();
		Log.v("CCODOA", "Building Names grabbed");
		/*
		 * displayList = new ArrayAdapter<String>(
		 * CreClosureOrDelayedOpenActivity.this,
		 * android.R.layout.simple_list_item_1, buildingNamesToBeDisplayed);
		 * Log.v("CCODOA", "ArrayAdapter created successfully");
		 */
		displayList = new ArrayAdapter<String>(getApplicationContext(),
				android.R.layout.simple_list_item_1, buildingNamesToBeDisplayed);
		// Add to list view
		displayList.getCount();
		Log.v("CCODOA", "Test of arrayAdapter completed");
		try {
			listView.setAdapter(displayList);
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
		Log.v("CCODOA", "ArrayAdapter set to listView");
		listView.setOnItemClickListener(new OnListViewClicked());
		Log.v("CCODOA", "OnClick set to listView");

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.cre_closure_or_delayed_open, menu);
		return true;
	}

	private class OnListViewClicked implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			Log.v("CCODOA", "In on clicked");
			building = (String) arg0.getItemAtPosition(arg2);
			getBuildingInfoForDisplay(fromWebService.get(arg2));
			// TODO Auto-generated method stub
			AlertDialog.Builder Info = new AlertDialog.Builder(
					CreClosureOrDelayedOpenActivity.this);
			Info.setTitle(building)
					.setItems(buildingInfoToBeDisplayed,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub

								}
							})
					.setNegativeButton(R.string.Exit,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
									dialog.cancel();
								}
							});
			Log.v("CCODOA", "Created Info");
			AlertDialog showInfo = Info.create();
			Log.v("CCODOA", "Displaying Info");
			showInfo.show();
		}

	}

	private void intializeArrayList(String token, String eventName,
			int eventYear) {
		try {

			Log.v("CCODOA", "Test 1");
			fromWebService.size();
			Log.v("CCODOA", "Test 2");
			fromWebService = LoginActivity.webService.getCREReport(token,
					eventName, eventYear);
			Log.v("CCODOA", "Test 3");
			for (Incident i : fromWebService) {
				Log.v("CCODOA", "Building Name: " + i.getBuildingName());
			}

			Log.v("CCODOA", "Building Names Displayed");
		} catch (TokenInvalidException e) {
			Intent i = new Intent(getBaseContext(), LoginActivity.class);
			startActivity(i);
		}

	}

	private void getBuildingNamesForDisplay() {
		for (int i = 0; i < fromWebService.size(); i++) {
			buildingNamesToBeDisplayed.add(fromWebService.get(i)
					.getBuildingName());
		}
	}

	private void getBuildingInfoForDisplay(Incident incident) {
		event += incident.getEventName();
		intialRequestDate += intialRequestDate;
		buildingstatus += incident.getBuildingStatus();
		glc += incident.getGeoLoc();
		address += incident.getBuildingAddress();
		state += incident.getState();
		type += incident.getBuildingType();
		pm += incident.getPMAttuid();
		assessment += incident.getAssessNotes();
		status += incident.getBuildingStatus();

		// Add to string array
		buildingInfoToBeDisplayed[0] = event;
		buildingInfoToBeDisplayed[1] = intialRequestDate;
		buildingInfoToBeDisplayed[2] = buildingstatus;
		buildingInfoToBeDisplayed[3] = glc;
		buildingInfoToBeDisplayed[4] = address;
		buildingInfoToBeDisplayed[5] = state;
		buildingInfoToBeDisplayed[6] = type;
		buildingInfoToBeDisplayed[7] = pm;
		buildingInfoToBeDisplayed[8] = assessment;
		buildingInfoToBeDisplayed[9] = status;

	}

}
